//配置路由

//引入路由
import Vue from 'vue'
import VueRouter from 'vue-router'

//引入store仓库
import store from '../store'

//使用插件
Vue.use(VueRouter)

// //引入路由组件
// import Register from '../pages/Register'
// import Login from '../pages/Login'
// import Search from '../pages/Search'
// import Detail from '../pages/Detail'
// import AddCartSuccess from '../pages/AddCartSuccess'
// import ShopCart from '../pages/ShopCart'
// import Trade from '../pages/Trade'
// import Pay from '../pages/Pay'
// import PaySuccess from '../pages/PaySuccess'
// import Center from '../pages/Center'

//二级路由
import MyOrder from '../pages/Center/myOrder'
import GroupOrder from '../pages/Center/groupOrder'

let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

VueRouter.prototype.plus = function(location, reslove, reject) {
	if (reslove && reject) {
		originPush.call(this, location, reslove, reject)
	} else {
		originPush.call(this, location, () => {}, () => {});
	}
}
VueRouter.prototype.replace = function(location, reslove, reject) {
	if (reslove && reject) {
		originReplace.call(this, location, reslove, reject)
	} else {
		originReplace.call(this, location, () => {}, () => {});
	}
}

//配置路由
let router = new VueRouter({
	routes: [{
			path: '/home',
			//路由懒加载
			component: ()=>import('../pages/Home'),
			//路由元信息
			meta: {
				show: true
			}
		},
		{
			name: 'search',
			// :keyword?  这里的？表示params参数可传可不传 相当于正则匹配 
			// 但是如果keyword参数传递的是空值，会导致路径出错 可以用undefined解决
			path: '/search/:keyword?',
			component:()=>import('../pages/Search'),
			meta: {
				show: true
			}
		},
		{
			name: 'Register',
			path: '/register',
			component:()=>import('../pages/Register'),
			meta: {
				show: false
			}
		},
		{
			name: 'Login',
			path: '/login',
			component:()=>import('../pages/Login'),
			meta: {
				show: false
			},
		},
		{
			name: "Detail",
			path: '/detail/:skuid',
			component:()=>import('../pages/Detail'),
			meta: {
				show: true
			}
		},
		{
			name: "ShopCart",
			path: '/cartlist',
			component:()=>import('../pages/ShopCart'),
			meta: {
				show: true
			}
		},
		{
			name: "AddCartSuccess",
			path: '/addcartsuccess',
			component:()=>import('../pages/AddCartSuccess'),
			meta: {
				show: true
			}
		},
		{
			name: 'Trade',
			path: '/trade',
			component:()=>import('../pages/Trade'),
			meta: {
				show: true
			},
			//路由独享守卫
			beforeEnter(to,from,next) {
				if(from.path=='/cartlist'){
					next()
				}else{
					next(false)
				}
			}
		},
		{
			name: 'Pay',
			path: '/pay',
			component:()=>import('../pages/Pay'),
			meta: {
				show: true
			},
			beforeEnter(to,from,next) {
				if(from.path=='/trade'){
					next()
				}else{
					next(false)
				}
			}
		},
		{
			name: 'PaySuccess',
			path: '/paysuccess',
			component:()=>import('../pages/PaySuccess'),
			meta: {
				show: true
			},
			beforeEnter(to,from,next) {
				if(from.path=='/pay'){
					next()
				}else{
					next(false)
				}
			}
		},
		// Center
		{
			name: 'Center',
			path: '/center',
			component:()=>import('../pages/Center'),
			meta: {
				show: true
			},
			redirect:'/center/myorder',
			children:[
				{
					name:'groupOrder',
					path:'grouporder',
					component:GroupOrder,
					meta:{show:true}
				},
				{
					name:'myOrder',
					path:'myorder',
					component:MyOrder,
					meta:{show:true}
				},
				
			]
		},
		//重定向
		{
			path: '*',
			redirect: '/home',
		}
	],
	//设置页面跳转后的位置
	scrollBehavior(to, from, savedPosition) {
		// 始终滚动到顶部
		return {
			y: 0
		}
	},
})

router.beforeEach(async (to, from, next) => {
	// next()
	//这里可以使用vuex中的数据，但是需要先import引入store
	let token = store.state.user.token
	let name = store.state.user.userInfo.name
	if (token) {
		if (to.path == '/login') {
			next(false)
		} else {
			if (name) {
				next()
			} else {
				//获取用户信息
				try {
					await store.dispatch('getUserInfo')
					next()
				} catch (e) {
					//TODO handle the exception
					// await store.dispatch('getUserInfo')
					store.state.user.token = ''
					store.state.userInfo = {}
					localStorage.removeItem('TOKEN')
					await store.dispatch('userLogout')
					next('/login')
					console.log(e.message)

				}
			}
		}
	} else {
		let toPath = to.path
		//没有登录不能去交易相关、支付相关、个人中心
		if(toPath.indexOf('/trade')!=-1||toPath.indexOf('/center')!=-1||toPath.indexOf('/pay')!=-1){
			//把想去但是没有去的页面路由通过query参数传递
			next(`/login?redirect=${toPath}`)
		}else{
			next()
		}
	}
	// console.log(store)
	// console.log(to, from)
})

export default router
